package com.ricohgag.controller.home;

import com.ricohgag.entity.Goods;
import com.ricohgag.entity.ShopAd;
import com.ricohgag.entity.VipUser;
import com.ricohgag.service.GoodsService;
import com.ricohgag.service.ShopAdService;
import com.ricohgag.service.VipUserService;
import com.ricohgag.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author ricohgag
 * @date 2018/9/3 18:06
 */
@Controller
@RequestMapping("/home/index")
public class HomeIndexController {
    @Autowired
    private VipUserService vipUserService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ShopAdService shopAdService;

    @RequestMapping("/index")
    public ModelAndView index() throws Exception{
        ModelAndView mav = new ModelAndView("/home/index/index");

        List<Goods> hotGoodsList = goodsService.selectGoodsHot();
        List<Goods> newGoodsList = goodsService.selectGoodsNew();

        List<ShopAd> shopAdDataList = shopAdService.selectShopAdAll();

        System.err.println("shopAdDataList: "+shopAdDataList);
        mav.addObject("hotGoodsList", hotGoodsList);
        mav.addObject("newGoodsList", newGoodsList);
        mav.addObject("shopAdDataList", shopAdDataList);

        return mav;
    }

    @RequestMapping("/login")
    public String toLogin() throws Exception{
        return "/home/index/login";
    }

    @RequestMapping("/register")
    public String toRegister() throws Exception{
        return "/home/index/register";
    }


    @RequestMapping("/doLogin")
    @ResponseBody
    public Msg doLogin(String username, String password, HttpServletRequest request) throws Exception{
        Msg msg = new Msg();
        VipUser vipUser = vipUserService.checkVipUser(username, password);

        if(vipUser == null){
            msg.setStatus(0);
            msg.setMsg("账号或密码不存在，请重新输入");
        } else {
            vipUser.setLastIp(request.getRemoteHost());
            vipUser.setLastLoginTime(LocalDateTime.now());
            request.getSession().setAttribute("vipUserSession", vipUser);
            msg.setMsg("登录成功");
            msg.setStatus(1);
        }
        return msg;
    }

    @RequestMapping("/doRegister")
    @ResponseBody
    public Msg doRegister(VipUser vipUser) throws Exception{
        Msg msg = new Msg();
        //判断是否有当前用户名
        if(vipUserService.selectByName(vipUser.getUsername()) != null){
            msg.setStatus(0);
            msg.setMsg("当前用户名已注册，请重新注册");

        } else {
            LocalDateTime addTime = LocalDateTime.now();
            vipUser.setAddTime(addTime);
            vipUser.setIsState(1);

            vipUserService.insertVipUser(vipUser);

            msg.setStatus(1);
            msg.setMsg("用户注册成功。");
        }

        return msg;

    }

    @RequestMapping("/exit")
    public String exit(HttpServletRequest request){
        request.getSession().removeAttribute("vipUserSession");
        return "redirect:/home/index/index";
    }



}
